/*
 * CurrentThread.h
 *
 *  Created on: 2021年4月5日
 *      Author: san
 */

#ifndef BASE_CURRENTTHREAD_H_
#define BASE_CURRENTTHREAD_H_

#include <cstdint>
#include <string>

namespace san {

namespace CurrentThread
{
// internal
extern __thread int t_cachedTid;
extern __thread char t_tidString[32];
extern __thread int t_tidStringLength;
extern __thread const char* t_threadName;
void cacheTid();
inline int tid() {
  if (__builtin_expect(t_cachedTid == 0, 0)) {
    cacheTid();
  }
  return t_cachedTid;
}

inline const char* tidString()  // for logging
{
  return t_tidString;
}

inline int tidStringLength()  // for logging
{
  return t_tidStringLength;
}

inline const char* name() { return t_threadName; }
}

} /* namespace san */

#endif /* BASE_CURRENTTHREAD_H_ */
